﻿Public Class FormaPagoCuotas

End Class

Namespace BC
  ''' <summary>
  ''' Clase utilizada en interfaz para bindear las cuotas en la pantalla de "forma de pago" de la factura.
  ''' </summary>
  ''' <remarks></remarks>
  Public Class PagoCuota


    Private _Porcentaje As Double
    Public Property Porcentaje() As Double
      Get
        Return _Porcentaje
      End Get
      Set(ByVal value As Double)
        _Porcentaje = value
      End Set
    End Property


    Private _Monto As Double
    Public Property Monto() As Double
      Get
        Return _Monto
      End Get
      Set(ByVal value As Double)
        _Monto = value
      End Set
    End Property

    Private _Fecha As DateTime
    Public Property Fecha() As DateTime
      Get
        Return _Fecha
      End Get
      Set(ByVal value As DateTime)
        _Fecha = value
      End Set
    End Property

  End Class

  Public Class lstPagosCuotas
    Inherits List(Of PagoCuota)
    Function GetPorcentajeTotal() As Double
      Return (From p In Me Select p.Porcentaje).Sum
    End Function
    Function GetMontoTotal() As Double
      Return (From p In Me Select p.Monto).Sum
    End Function
    Function MontoValido(ByVal pTotalFactura As Double) As Boolean
      If CInt(GetMontoTotal()) <> CInt(pTotalFactura) Then
        Return False
      Else
        Return True
      End If
    End Function
    Function PorcentajeValido() As Boolean
      If CInt(GetPorcentajeTotal()) = 100 Then
        Return True
      Else
        Return False
      End If
    End Function

    Public Sub CargarConCuotas(ByVal pCantidadCuotas As Integer, ByVal pMonto As Double, ByVal pFechaInicio As DateTime)
      Dim objPagoCuota As PagoCuota
      'Dim dblPorcenUna As Double
      Dim dblMontoUna As Double
      Dim datFechaIni As DateTime = pFechaInicio
      Try
        'dblPorcenUna = 100 / pCantidadCuotas
        dblMontoUna = pMonto / pCantidadCuotas
        For i As Integer = 1 To pCantidadCuotas
          objPagoCuota = New PagoCuota
          With objPagoCuota
            .Fecha = datFechaIni
            .Monto = Math.Round(dblMontoUna, 2, System.MidpointRounding.ToEven)
            '.Porcentaje = Math.Round(dblPorcenUna, 2, System.MidpointRounding.ToEven)
            .Porcentaje = (.Monto * 100) / pMonto
          End With
          datFechaIni = datFechaIni.AddMonths(1)
          Me.Add(objPagoCuota)
        Next
        RedondearCuotas(pMonto)
      Catch ex As Exception
        Throw ex
      End Try
    End Sub

    Private Sub RedondearCuotas(ByVal pMontoOriginal As Double)
      Dim dMontoActual As Double
      Dim dPorceActual As Double
      Try
        dMontoActual = Me.Sum(Function(o) o.Monto)
        dPorceActual = Me.Sum(Function(o) o.Porcentaje)
        If pMontoOriginal > dMontoActual Then
          With Me.OrderBy(Function(o) o.Fecha).LastOrDefault
            .Monto += pMontoOriginal - dMontoActual
            .Porcentaje += 100 - dPorceActual
          End With
        End If
      Catch ex As Exception
        Throw ex
      End Try
    End Sub

    Public Sub ActualizarMonto(ByVal pMontoTotal As Double)
      For Each unaCuota In Me
        With unaCuota
          .Monto = (.Porcentaje * pMontoTotal) / 100
        End With
      Next
    End Sub

  End Class
End Namespace